/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.engine.servicetime;

import java.util.Calendar;
import java.util.logging.Logger;

import org.aergia.vinny.core.ApiResult;
import org.aergia.vinny.core.enums.DayOfWeek;
import org.aergia.vinny.database.Database;
import org.aergia.vinny.database.pojo.OperatingHours;
import org.aergia.vinny.database.pojo.Scan;
import org.aergia.vinny.database.pojo.Service;
import org.aergia.vinny.util.DateUtil;

/**
 * @author fred
 */
public final class OperatingHoursCalculator extends ServiceTimeCalculatorSkeleton {

	public OperatingHoursCalculator(final Database database) {
		super(database, Logger.getLogger(OperatingHoursCalculator.class.getCanonicalName()));
	}

	@Override
	protected Calendar getAppropriateEndTime(final Scan scan) {
		Calendar endTime = null;
		final Calendar closingTime = this.calculateClosing(scan);
		final Calendar midnightOfService = this.calculateMidnight(scan);
		if (DateUtil.isBefore(scan.getScanTime(), closingTime)) {
			endTime = closingTime;
		} else {
			endTime = midnightOfService;
		}
		return endTime;
	}

	@Override
	protected boolean isInServiceTime(final Service service) {
		return this.isInOperatingHours(service);
	}

	@Override
	protected boolean isScanOutOfRange(final Scan scan) {
		return DateUtil.isBeforeMidnightYesterday(scan.getScanTime());
	}

	@Override
	protected boolean isScanTimesOk(final Scan startScan, final Scan endScan) {
		return DateUtil.isSameDay(startScan.getScanTime(), endScan.getScanTime());
	}

	/**
	 * @param scan
	 * @return closing date
	 */
	private Calendar calculateClosing(final Scan scan) {
		final Calendar closing = DateUtil.getInstance(scan.getScanTime());
		final DayOfWeek day = DateUtil.getDayOfWeek(closing);
		final ApiResult<OperatingHours> result = this.database.getOperatingHours(day);
		if (result.isError()) {
			return this.calculateMidnight(scan);
		}
		closing.set(Calendar.HOUR_OF_DAY, result.getResult().getEndHour() / 60);
		closing.set(Calendar.MINUTE, result.getResult().getEndHour() % 60);
		closing.set(Calendar.SECOND, 0);
		closing.set(Calendar.MILLISECOND, 0);
		return closing;
	}

	/**
	 * @param scan
	 * @return
	 */
	private Calendar calculateMidnight(final Scan scan) {
		final Calendar midnight = DateUtil.getInstance(scan.getScanTime());
		midnight.add(Calendar.DAY_OF_MONTH, 1);
		midnight.set(Calendar.HOUR_OF_DAY, 0);
		midnight.set(Calendar.MINUTE, 0);
		midnight.set(Calendar.SECOND, 0);
		midnight.set(Calendar.MILLISECOND, 0);
		return midnight;
	}

	private boolean isInOperatingHours(final Service service) {
		if (DateUtil.isSameDay(service.getStartTime(), service.getEndTime())) {
			final DayOfWeek day = DateUtil.getDayOfWeek(service.getStartTime());
			final ApiResult<OperatingHours> hoursResult = this.database.getOperatingHours(day);
			if (!hoursResult.isError() && (hoursResult.getResult() != null)) {
				final OperatingHours hours = hoursResult.getResult();

				final int startMinutes = DateUtil.getMinutesOfDay(service.getStartTime());
				if (startMinutes < hours.getStartHour()) {
					service.setComment("Start time before opening hour of " + DateUtil.formatHour(hours.getStartHour()));
					return false;
				}

				final int endMinutes = DateUtil.getMinutesOfDay(service.getEndTime());
				if (endMinutes > hours.getEndHour()) {
					service.setComment("Stop time after closing hour of " + DateUtil.formatHour(hours.getEndHour()));
					return false;
				}
				return true;
			}
		}
		return false;
	}
}
